<template>
    <div >
      <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 300px; padding: 20px; border-radius: 10px">
        <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>商 家 登 录</b></div>
        <el-form  :model="admin"  ref="userForm" :rules="rules">
<!--          用户名-->
          <el-form-item prop="username">
            <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="admin.username"></el-input>
          </el-form-item>
<!--          密码-->
          <el-form-item prop="password">
            <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="admin.password"></el-input>
          </el-form-item>
<!--          登陆按钮-->
          <el-form-item style="margin: 10px 0; text-align: right">
            <el-button style="position: absolute;left: 0;top:50%;transform: translate(-15%,-50%)" type="warning" size="small"  autocomplete="off" @click="$router.push('/login')">切换至用户版登录</el-button>
            <el-button style="position: absolute;left: 70%;top:50%;transform: translate(-70%,-50%)" type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
            <el-button type="info" size="small" autocomplete="off" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
import {baseURL} from "../../public/urlConfig";
import axios from "axios";

export default{
  name: "Login",
  data() {
    return {
      admin:{},
      //验证规则
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1,max:16, message: '用户名长度不合法', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1,max:16,  message: '密码长度不合法', trigger: 'blur'}
        ],
      }
    }
  },

  methods: {
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
          var url = baseURL+"/admin/login";
          axios.get(url,{
            params:{
              adminname:this.admin.username,
              adminpwd:this.admin.password
            }
          }).then((res)=>{
            //res.data 表示接口返回的数据
            var vo = res.data;
            console.log(vo);
            if (vo.code === 10000) {
              localStorage.setItem("adminname", vo.data.adminname)  // 存储用户信息到浏览器
              sessionStorage.setItem("admintoken",vo.msg)
              localStorage.setItem("adminId",vo.data.adminId)
              // 动态设置当前用户的路由
              this.$message.success("登录成功")
              this.$router.push("/admin");  // 若不是网站内部跳转过来的，登陆成功后进入网站首页
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      });
    },
    reset(){
      this.$refs.userForm.resetFields();
    },

  }

}
</script>

<style lang="less" scoped>
.admin_container{
  background-color: #81f1e1;
  height: 100%;
}
</style>
